package com.njxxted.activiti.modules.process.defintion.controller;

import com.njxxted.activiti.common.exception.CustomerException;
import com.njxxted.activiti.common.response.ResponseVo;
import com.njxxted.activiti.modules.process.defintion.entity.ActNodeConfigEntity;
import com.njxxted.activiti.modules.process.defintion.service.IActDefinitionService;
import com.njxxted.activiti.modules.process.extend.entity.ExtendActInstanceEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 描述：流程定义 controller
 * <p>
 * 作者：Ostrich Hu
 * 时间：2019/9/3 14:55 星期二
 */
@Api(tags = "流程定义")
@RestController
@RequestMapping(value = "/act/rest/definition")
public class ActDefinitionController {

    private final IActDefinitionService iActDefinitionService;

    public ActDefinitionController(IActDefinitionService iActDefinitionService) {
        this.iActDefinitionService = iActDefinitionService;
    }

    @ApiOperation(value = "根据模型id获取流程图")
    @GetMapping("/image/{modelId}")
    public void getDefinitionImage(HttpServletResponse response, @PathVariable String modelId) {
        try {
            iActDefinitionService.getDefinitionImage(modelId, response);
        } catch (IOException e) {
            throw new CustomerException("获取资源图片异常", e);
        }
    }

    @ApiOperation(value = "获取流程执行路劲图")
    @GetMapping("/image/route/{instanceId}")
    public void getFlowRouteImage(HttpServletResponse response, @PathVariable String instanceId) {
        iActDefinitionService.getFlowExecutionRouteImage(instanceId, response);
    }

    @ApiOperation(value = "设置流程定义信息", notes = "设置流程定义信息")
    @PostMapping("setting")
    public ResponseVo processDefinitionSettings(HttpServletRequest request) {
        return iActDefinitionService.processDefinitionSettings(request);
    }

    @ApiOperation(value = "查询流程定义配置信息")
    @GetMapping("setting/info")
    public ResponseVo getProcessDefinitionSettings(ActNodeConfigEntity param) {
        return ResponseVo.ok(iActDefinitionService.getProcessDefinitionSettings(param));
    }

    @ApiOperation(value = "发起流程实例")
    @PostMapping("start/instance")
    public ResponseVo startInstanceByDefinition(HttpServletRequest request) {
        ExtendActInstanceEntity extendActInstanceEntity = iActDefinitionService.startInstanceByDefinition(request);
        return null != extendActInstanceEntity ? ResponseVo.ok(extendActInstanceEntity) : ResponseVo.error("发起流程实例失败");
    }

    @ApiOperation(value = "更新发起的流程实例")
    @PostMapping("upd/instance")
    public ResponseVo updInstanceByDefinition(HttpServletRequest request) {
        ExtendActInstanceEntity extendActInstanceEntity = iActDefinitionService.updInstanceByDefinition(request);
        return null != extendActInstanceEntity ? ResponseVo.ok(extendActInstanceEntity) : ResponseVo.error("更新发起的流程实例失败");
    }

}
